<html>
<head><meta charset="utf-8"><title>proc_macro_attribute and syntax analysis · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html">proc_macro_attribute and syntax analysis</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234156458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/proc_macro_attribute%20and%20syntax%20analysis/near/234156458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html#234156458">(Apr 12 2021 at 13:23)</a>:</h4>
<p>We would like to use <code>proc_macro_attribute</code> to support a superset of Rust syntax (for functional specifications) inside function bodies. For example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[predicate]</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="o">==&gt;</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// note ==&gt; as an implication operator</span>
</code></pre></div>
<p>This kind of parsing works fine inside attribute arguments. However, we just found out that rustc seems to perform syntax analysis of the item body before the macro attribute is invoked. So in the example above, if <code>predicate</code> is implemented to simply return an empty <code>TokenStream</code>, the compiler still raises an error about the invalid syntax, even before the attribute is invoked. Is this the indended functionality? Can syntax analysis be delayed until after all macro attributes have executed?</p>



<a name="234158728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/proc_macro_attribute%20and%20syntax%20analysis/near/234158728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html#234158728">(Apr 12 2021 at 13:36)</a>:</h4>
<p>No, all input to attribute macros needs to parse as valid Rust code</p>



<a name="234160765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/proc_macro_attribute%20and%20syntax%20analysis/near/234160765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html#234160765">(Apr 12 2021 at 13:47)</a>:</h4>
<p>Ok, thanks. We can work around this issue (e.g. by requiring the function body to be surrounded in a dummy <code>macro!({ ... })</code>. Is there a reason the delay cannot happen? Is it too detrimental to performance?</p>



<a name="234161503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/proc_macro_attribute%20and%20syntax%20analysis/near/234161503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html#234161503">(Apr 12 2021 at 13:51)</a>:</h4>
<p>Macro expansion requires performing name resolution, which requires the files to be parsed first, so there's not really a way to avoid emitting parse errors first</p>



<a name="234161717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/proc_macro_attribute%20and%20syntax%20analysis/near/234161717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/proc_macro_attribute.20and.20syntax.20analysis.html#234161717">(Apr 12 2021 at 13:52)</a>:</h4>
<p>I suppose you could rely on parse error recovery, and only emit the parse errors after expansion, but specifying how exactly that's supposed to work sounds like a nightmare</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>